# coding=utf-8

from socket import *
import pygame
from pygame.locals import *
from sys import exit
import configure


PORT = 21567
BUFSIZE = 1024
TARGET_RECV_ADDR = (configure.TARGET_HOST, configure.TARGET_RECV_PORT)

print TARGET_RECV_ADDR

# tcpRecvSock = socket(AF_INET, SOCK_STREAM)
# tcpRecvSock.bind(('localhost', PORT + 1))
# tcpRecvSock.listen(5)
targetRecvSocket = socket(AF_INET, SOCK_STREAM)
targetRecvSocket.connect(TARGET_RECV_ADDR)


pygame.init()
SCREEN_SIZE = (800, 600)
screen = pygame.display.set_mode(SCREEN_SIZE, 0, 32)
font = pygame.font.SysFont("consolas", 16)
font_height = font.get_linesize()
event_text = []

while True:
    event = pygame.event.wait()
    if event.type == MOUSEMOTION:
        event_text.append(str(event))
        targetRecvSocket.send(str(event.pos))
    elif event.type == MOUSEBUTTONDOWN:
        event_text.append(str(event))
        l = list(event.pos)
        l.append(1)
        targetRecvSocket.send(str(l))
    event_text = event_text[-SCREEN_SIZE[1] / font_height:]
    if event.type == QUIT:
        targetRecvSocket.close()
        exit()

    screen.fill((255, 255, 255))

    y = SCREEN_SIZE[1] - font_height
    for text in reversed(event_text):
        screen.blit(font.render(text, True, (0, 0, 0)), (0, y))
        y -= font_height

    pygame.display.update()